package modules.lobby.data
{
	import modules.GameDispatcher;
	import modules.lobby.LobbyEvent;


	public class TableDataListSortFilter
	{
		private static var isInit:Boolean = false;

		//排序方案
		private static var sortPlanList:Array = [];

		public static var sortType:String = "roomIdText";

		[Bindable]
		public static var useed:int = 0;

		[Bindable]
		public static var sortIconX:int = 66;

		private static function init():void
		{
			if (!isInit)
			{
				//排序名称 排序方法列表 使用第几个排序方法 是否开启这个排序
				sortPlanList.push({name: "roomIdText", func: sortOnName, useed: true, isSort: false});
				sortPlanList.push({name: "blind", func: sortOnBlinds, useed: false, isSort: false});
				sortPlanList.push({name: "buyIn", func: sortOnBuyAmount, useed: false, isSort: false});
				sortPlanList.push({name: "player", func: sortOnseatedNum, useed: false, isSort: false});
				sortPlanList.push({name: "tileNum", func: function(a:NormalTableModel, b:NormalTableModel):Number
				{
					return a.tileCount - b.tileCount;
				}, useed: false, isSort: false});
			}
			isInit = true;
		}

		public static function clickTableTitleHandler(sortName:String = "roomIdText"):void
		{
			init();
			//修改排序方案
			var length:int = sortPlanList.length;
			var i:int = 0;
			var sortPlan:Object = null;
			for (i = 0; i < length; i++)
			{
				sortPlan = sortPlanList[i];
				if (sortPlan.name == sortName)
				{
					sortPlan.isSort = true;
					sortPlan.useed = !sortPlan.useed;
					//把该排序方案放到最后
					sortPlanList.splice(i, 1);
					sortPlanList.push(sortPlan);

					sortType = sortName;
					useed = sortPlan.useed;

					break;
				}
			}

			doSortFilter();
		}

		/**
		 * 按照排序方案排序
		 **/
		public static function sortOnPlanList():void
		{
			var i:int = 0;
			var length:int = sortPlanList.length;
			var sortPlan:Object;
			for (i = 0; i < length; i++)
			{
				sortPlan = sortPlanList[i];
				if (sortPlan.isSort)
				{
					LobbyDataModel.cashTableShowData.sort(sortPlan.func);
					if (!sortPlan.useed)
					{
						LobbyDataModel.cashTableShowData = LobbyDataModel.cashTableShowData.reverse();
					}
				}
			}
		}

		/**
		 * 按名称排序
		 **/
		private static function sortOnName(a:NormalTableModel, b:NormalTableModel):Number
		{
			return a.tableID.toNumber() - b.tableID.toNumber();
		}

		/**
		 * 按大小盲注排序
		 **/
		private static function sortOnBlinds(a:NormalTableModel, b:NormalTableModel):Number
		{
			return a.minBlinds + a.maxBlinds - (b.minBlinds + b.maxBlinds);
		}

		/**
		 * 按最小最大买入排序
		 **/
		private static function sortOnBuyAmount(a:NormalTableModel, b:NormalTableModel):Number
		{
			return (a.minBuyAmount + a.maxBuyAmount) - (b.minBuyAmount + b.maxBuyAmount);
		}

		/**
		 * 按押注出牌时间排序
		 **/
		private static function sortOnTime(a:NormalTableModel, b:NormalTableModel):Number
		{
			return (a.responseTime + a.discardTime) - (b.responseTime + b.discardTime);
		}

		/**
		 * 按在座玩家数排序
		 **/
		private static function sortOnseatedNum(a:NormalTableModel, b:NormalTableModel):Number
		{
			if (a.seatedNum != b.seatedNum)
				return a.seatedNum - b.seatedNum;
			return a.maxNum - b.maxNum;
		}

		/**
		 * 过滤规则数据
		 **/
		public static var filterData:Object;

		/**
		 * 是否开启过滤
		 **/
		[Bindable]
		public static var isFilter:Boolean = false;

		/**
		 * 过滤
		 **/
		public static function filterTableList():void
		{
			LobbyDataModel.cashTableShowData = LobbyDataModel.cashTableShowData.filter(function(element:NormalTableModel, index:int, arr:Array):Boolean
			{
				if (filterData != null && isFilter)
				{
//					buyIn:buyInCB.value,playerNum:playerNumCB.value,cardNum:cardNumCB.value}
					if (filterData.tableName != "" && filterData.tableName != null)
					{
						if (element.tableName.indexOf(filterData.tableName) == -1)
						{
							return false;
						}
					}
					if (filterData.blinds && filterData.blinds.minBlinds && filterData.blinds.maxBlinds)
					{
						if (element.minBlinds != filterData.blinds.minBlinds || element.maxBlinds != filterData.blinds.maxBlinds)
						{
							return false;
						}
					}
					if (filterData.buyIn && filterData.buyIn.minBuyAmount && filterData.buyIn.maxBuyAmount)
					{
						if (element.minBuyAmount != filterData.buyIn.minBuyAmount || element.maxBuyAmount != filterData.buyIn.maxBuyAmount)
						{
							return false;
						}
					}
					if (filterData.playerNum)
					{
						if (element.seatedNum != filterData.playerNum)
						{
							return false;
						}
					}
					if (filterData.cardNum)
					{
						if (element.tileCount != filterData.cardNum)
						{
							return false;
						}
					}
					if(filterData.currency)
					{
						if(element.currency != filterData.currency)
						{
							return false;
						}
					}
				}
				return true;
			});
		}

		public static function doSortFilter():void
		{
			LobbyDataModel.cashTableShowData = LobbyDataModel.cashTableData.concat();

			filterTableList();
			sortOnPlanList();

			GameDispatcher.instance.dispatchEvent(new LobbyEvent(LobbyEvent.REFRESH_TABLE_LIST_UI));
		}

	}



}
